package com.wlz.splitpackage.customlength;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wlz
 * @date 2022-07-09  3:42 下午
 */
@Slf4j
public class CLengthPackageNettyServerHandler extends SimpleChannelInboundHandler<MyMessageProtocol> {

    private int count;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MyMessageProtocol myMessageProtocol) throws Exception {
        log.info("====== 服务端接收到的消息如下: =====");

        log.info("长度: {}", myMessageProtocol.getLen());
        log.info("内容: {}", new String(myMessageProtocol.getContent(), CharsetUtil.UTF_8));

        log.info("服务端接收到消息包数量: {}", ++this.count);

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
